﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="createDialog" class="crudDialog">
	<form id="createForm" method="post">
		<div class="form-group">
			<label for="name">名称</label>
			<input id="name" type="text" class="form-control" name="name" maxlength="32">
		</div>
		<div class="form-group">
			<label for="model">型号</label>
			<input id="model" type="text" class="form-control" name="model" maxlength="32">
		</div>
		<div class="form-group">
			<label for="spec">规格</label>
			<input id="spec" type="text" class="form-control" name="spec" maxlength="20">
		</div>
		<div class="form-group">
			<span>
				<select id="categoryId" name="categoryId">
					<option value="0">请选择分类</option>
					<c:forEach var="partCategorie" items="${partCategories}">
					<option value="${partCategorie.categoryId}">${partCategorie.name}</option>
					</c:forEach>
				</select>
			</span>
		</div>
		<div class="form-group">
			<input id="categoryName" type="hidden" class="form-control" name="categoryName" maxlength="20">
		</div>
		<div class="form-group">
			<label for="unit">单位</label>
			<input id="unit" type="text" class="form-control" name="unit" maxlength="20">
		</div>
		<div class="form-group">
			<label for="brank">品牌</label>
			<input id="brank" type="text" class="form-control" name="brank" maxlength="20">
		</div>
		<div class="form-group">
			<label for="price">单价</label>
			<input id="price" type="text" class="form-control" name="price" maxlength="20">
		</div>
		<div class="form-group">
			<label for="cremark">备注</label>
			<input id="cremark" type="text" class="form-control" name="cremark" maxlength="50">
		</div>
		<div class="form-group text-right dialog-buttons">
			<a class="waves-effect waves-button" href="javascript:;" onclick="createSubmit();">保存</a>
			<a class="waves-effect waves-button" href="javascript:;" onclick="createDialog.close();">取消</a>
		</div>
	</form>
</div>
<script>
function updateForm() {
	var categoryName =  $("#categoryId").find("option:selected").text();
	$("#categoryName").val(categoryName);
}

function createSubmit() {
	updateForm();
    $.ajax({
        type: 'post',
        url: '${basePath}/base/part/create.do',
        data: $('#createForm').serialize(),
        beforeSend: function() {
            if ($('#model').val() == '' ) {
                $('#model').focus();
                return false;
            }
            if ($('#spec').val() == '' ) {
                $('#spec').focus();
                return false;
            }
        },
        success: function(result) {
			if (result.code != 1) {
				if (result.data instanceof Array) {
					$.each(result.data, function(index, value) {
						$.confirm({
							animation: 'rotateX',
							closeAnimation: 'rotateX',
							title: false,
							content: value.errorMsg,
							buttons: {
								confirm: {
									text: '确认',
									btnClass: 'waves-effect waves-button waves-light'
								}
							}
						});
					});
				} else {
						$.confirm({
							theme: 'dark',
							animation: 'rotateX',
							closeAnimation: 'rotateX',
							title: false,
							content: result.data.errorMsg || result.data,
							buttons: {
								confirm: {
									text: '确认',
									btnClass: 'waves-effect waves-button waves-light'
								}
							}
						});
				}
			} else {
				createDialog.close();
				$table.bootstrapTable('refresh');
			}
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
			$.confirm({
				theme: 'dark',
				animation: 'rotateX',
				closeAnimation: 'rotateX',
				title: false,
				content: textStatus,
				buttons: {
					confirm: {
						text: '确认',
						btnClass: 'waves-effect waves-button waves-light'
					}
				}
			});
        }
    });
}
</script>